<template>
  <div class="mod-menu">
    <el-form :inline="true" :model="dataForm">
      <el-form-item v-show="!selectMode">
        <el-button size="mini" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button size="mini" type="primary" @click="SynchronousMaterial()">同步素材</el-button>
      </el-form-item>
    </el-form>
    <div v-loading="dataListLoading">
      <div v-for="item in dataList" :key="item.mediaId" class="card" @click="onSelect(item)">
        <el-image v-if="fileType=='image'" class="card-image" :src="item.local_url" fit="contain" lazy />
        <div v-else class="card-preview">
          <div v-if="fileType=='voice'" class="card-preview-icon el-icon-microphone" />
          <div v-if="fileType=='video'" class="card-preview-icon">
            <video :src="item.local_url" width="100%" controls />
          </div>
          <!-- <div class="card-preview-text">管理后台不支持预览<br>微信中可正常播放</div> -->
        </div>
        <div class="card-footer">
          <div class="text-cut-name">{{ item.name }}</div>
          <div>{{ $moment(item.updateTime).calendar() }}</div>
          <div v-show="!selectMode" class="flex justify-between align-center">
            <el-button v-if="item.status === 1" v-clipboard:copy="item.media_id" v-clipboard:success="onCopySuccess" v-clipboard:error="onCopyError" size="mini" type="text" icon="el-icon-copy-document">
              复制media_id
            </el-button>
            <el-button v-if="item.status === 0" size="mini" type="text" icon="el-icon-cloudy" @click="UpWxHandle(item.id)">同步微信服务器</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteHandle(item.id)">删除</el-button>
          </div>
        </div>
      </div>
    </div>
    <el-pagination :current-page="pageIndex-1>0?pageIndex-1:1" :page-sizes="[20]" :page-size="20" :total="totalCount" layout="total, prev,pager, next, jumper" @current-change="currentChangeHandle" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="onChange" />
  </div>
</template>
<script>
import { delNewsList, fetchNewsList, mediaInitList, mediaUpWx, newsSynchronous } from '../../api/wx-news-template'
import AddOrUpdate from './material-file-add-or-update'
export default {
  name: 'MaterialFile',
  components: {
    AddOrUpdate
  },
  props: {
    fileType: { // image、voice、video
      type: String,
      default: 'image'
    },
    selectMode: { // 是否选择模式，选择模式下点击素材选中，不可新增和删除
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dataForm: {},
      addOrUpdateVisible: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 20,
      totalCount: 0,
      dataListLoading: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      if (!this.dataList.length) {
        this.getDataList()
      }
    },
    getDataList() {
      if (this.dataListLoading) return
      this.dataListLoading = true
      mediaInitList({
        'page': this.pageIndex,
        'OfficialaccountWechatMedia[type]': this.fileType
      }).then(data => {
        this.dataList = data.data.item
        this.totalCount = data.data.total_count
        this.pageIndex++
        this.dataListLoading = false
      })
    },
    UpWxHandle(id) {
      mediaUpWx({ id: id }).then((data) => {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500
        })
        this.getDataList()
      })
    },
    SynchronousMaterial() {
      newsSynchronous().then((data) => {
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500
        })
        this.getDataList()
      }).catch((err) => {
        this.$message({
          message: err.message || '操作失败',
          type: 'error'
        })
      })
    },
    // 新增 / 修改
    addOrUpdateHandle() {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(this.fileType)
      })
    },
    onSelect(itemInfo) {
      if (!this.selectMode) return
      this.$emit('selected', itemInfo)
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定对[mediaId=${id}]进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delNewsList(id).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => this.onChange()
          })
        })
      })
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    onCopySuccess() {
      this.$message.success('已复制')
    },
    onCopyError(err) {
      console.log(err)
      this.$message.error('复制失败,可能是此浏览器不支持复制')
    },
    onChange() {
      this.pageIndex = 1
      this.dataListLoading = true
      this.getDataList()
      this.$emit('change')
    }

  }
}
</script>
<style scoped>
.card{
    width: 170px;
    display: inline-block;
    background: #FFFFFF;
    border: 1px solid #EBEEF5;
    box-shadow: 1px 1px 20px 0 rgba(0, 0, 0, 0.1);
    margin: 0 10px 10px 0;
    vertical-align: top;
    border-radius: 5px;
    box-sizing: border-box;
}
.card:hover{
    border: 2px solid #66b1ff;
    margin-bottom: 6px;
}
.card-image{
    line-height: 170px;
    max-height: 170px;
    width: 100%;
}
.card-preview{
    /* padding: 20px 0; */
    color: #d9d9d9;
    display: flex;
    justify-content: center;
    align-items: center;
}
.card-preview-icon{
    font-size: 30px;
    margin: 5px;
}
.card-preview-text{
    font-size: 12px;
}
.card-footer{
    color: #ccc;
    font-size: 12px;
    padding: 15px 10px;
}
</style>
